package br.com.amadeus.android.activities.home;

import br.com.amadeus.android.util.AccessMode;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;
import br.com.amadeus.android.util.PendingIntentService;
import br.com.amadeus.android.util.SchedulerSyncronize;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;

public class SettingsActivity extends PreferenceActivity{
	
	protected static final String CATEGORIA = "curso";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
		sincronize();
		sincronizeTimeline();
		sincronizeForum();
		notifyBeginDateCourses();
		notifyEndDateCourses();
		verifyAcessMode();
		
	}

	private void verifyAcessMode() {
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		int opcao = Integer.parseInt(getPrefs.getString("listPref", "1"));
		if (opcao == ConstantsAmadeusDroid.MODO_OFFLINE) {
			AccessMode.modifiedByUser = true;
		}else{
			AccessMode.modifiedByUser = false;
		}
		
	}

	private void notifyEndDateCourses() {
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		boolean notifyEndCourse = getPrefs.getBoolean("ckNotificarFimCurso", true);
		if (!notifyEndCourse) {
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piEndDt = PendingIntentService.getInstance().getEndDateIntent();
			alarm.cancel(piEndDt);
		}else{
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piEndDt = PendingIntentService.getInstance().getEndDateIntent();
			alarm.cancel(piEndDt);
			
			String valueEndDate = getPrefs.getString("listFimCurso", String.valueOf(ConstantsAmadeusDroid.START_FIM_COURSE));
			int minute = Integer.parseInt(valueEndDate);
			PendingIntent piEndDate = SchedulerSyncronize.startService(minute, SettingsActivity.this, alarm, ConstantsAmadeusDroid.SERVICE_ENDDATE);
			PendingIntentService.getInstance().setEndDateIntent(piEndDate);
		}
		
	}

	private void notifyBeginDateCourses() {
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		boolean notifyBeginCourse = getPrefs.getBoolean("ckNotificarIniCurso", true);
		if (!notifyBeginCourse) {
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piBegDt = PendingIntentService.getInstance().getBeginDateIntent();
			alarm.cancel(piBegDt);
		}else{
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piBegDt = PendingIntentService.getInstance().getBeginDateIntent();
			alarm.cancel(piBegDt);
			
			String valueBeginDate = getPrefs.getString("listInicioCurso", String.valueOf(ConstantsAmadeusDroid.START_INI_COURSE));
			int minute = Integer.parseInt(valueBeginDate);
			PendingIntent piBeginDate = SchedulerSyncronize.startService(minute, SettingsActivity.this, alarm, ConstantsAmadeusDroid.SERVICE_BEGINDATE);
			PendingIntentService.getInstance().setBeginDateIntent(piBeginDate);
		}
		
	}

	private void sincronizeTimeline() {
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		boolean notifyTimeline = getPrefs.getBoolean("ckNotificarTimeline", true);
		if (!notifyTimeline) {
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piTimeline = PendingIntentService.getInstance().getTimelineIntent();
			alarm.cancel(piTimeline);
		}else{
			
			//Cancela o serviço atual
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piTimeline = PendingIntentService.getInstance().getTimelineIntent();
			alarm.cancel(piTimeline);
			
			//Inicia novo serviço com o tempo definido pelo usuário
			String valueTimeline = getPrefs.getString("listTimeline", String.valueOf(ConstantsAmadeusDroid.START_SYNC_TIMELINE));
			int minute = Integer.parseInt(valueTimeline);
			PendingIntent piTimeline2 = SchedulerSyncronize.startService(minute, SettingsActivity.this, alarm, ConstantsAmadeusDroid.SERVICE_TIMELINE);
			PendingIntentService.getInstance().setTimelineIntent(piTimeline2);
		}
	}

	private void sincronizeForum() {
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		boolean notifyForum = getPrefs.getBoolean("ckNotificarRespostasForum", true);
		if (!notifyForum) {
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piForum = PendingIntentService.getInstance().getForumIntent();
			alarm.cancel(piForum);
			
		}else{
			
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piForum = PendingIntentService.getInstance().getForumIntent();
			alarm.cancel(piForum);
			
			String valueForum = getPrefs.getString("listForum", String.valueOf(ConstantsAmadeusDroid.START_SYNC_FORUM_MESSAGES));
			int minute = Integer.parseInt(valueForum);
			PendingIntent piForum2 = SchedulerSyncronize.startService(minute, SettingsActivity.this, alarm, ConstantsAmadeusDroid.SERVICE_FORUM);
			PendingIntentService.getInstance().setForumIntent(piForum2);
		}
		
		
	}

	private void sincronize() {
		
		SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
		boolean notifySincronize = getPrefs.getBoolean("ckNotificarSincronize", true);
		if (!notifySincronize) {
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piSincronize = PendingIntentService.getInstance().getSincronizeIntent();
			alarm.cancel(piSincronize);
		}else{
			
			//Cancela o serviço atual
			AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
			PendingIntent piSincronize = PendingIntentService.getInstance().getSincronizeIntent();
			alarm.cancel(piSincronize);
			
			//Inicia novo serviço com o tempo definido pelo usuário
			String valueSinc = getPrefs.getString("listSincronize", String.valueOf(ConstantsAmadeusDroid.START_SYNCRONIZE));
			int minute = Integer.parseInt(valueSinc);
			PendingIntent piSync = SchedulerSyncronize.startService(minute, SettingsActivity.this, alarm, ConstantsAmadeusDroid.SERVICE_SYNCRONIZE);
			PendingIntentService.getInstance().setSincronizeIntent(piSync);
		}
		
	}

}
